﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using projet_fichier_objet;
using FichierTxt;
using Outils;
using System.Collections.Generic;
using System.Data.SqlClient;


namespace App_Recherche
{
    public partial class Ajout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void submit_Click(object sender, EventArgs e)
        {
            
            Personne Interimaire = new Personne();
            //vérif saisie
            bool Erreur = Utils.CheckSaisi(NomAjout.Text);
            if (Erreur == false)
            {
                Interimaire.Nom = NomAjout.Text;
                MessageNom.Text = string.Empty;
                for (int i = 0; i < EmailAjout.Text.Length; i++)
                {
                    if (EmailAjout.Text[i] == '@')
                    {
                        Interimaire.Email = EmailAjout.Text;
                        MessageEmail.Text = string.Empty;
                        i = EmailAjout.Text.Length + 1;
                    }
                    else
                        MessageEmail.Text = "saisie incorrecte";
                }
                Interimaire.Ville = VilleAjout.Text;
                DateTime Madate = new DateTime();
                Madate.ToString("d");
                bool SaisieError = DateTime.TryParse(DateAjout.Text, out Madate);

                if (SaisieError == true)
                {
                    Interimaire.DateNaissance = Madate;
                    MessageDate.Text = string.Empty;

                    //----------DEBUT REQUETE

                    //string CnxInterimaire = "Data Source=PILOTT-LGZ;Initial Catalog=Interimaires;Integrated Security=True;Pooling=False";
                    SqlConnection SqlC = new SqlConnection(Utils.CnxInterimaire);

                    //-------------------------Ouverture
                    SqlC.Open();

                    SqlCommand SQLCmd = SqlC.CreateCommand();
                    SQLCmd.CommandText = " INSERT INTO Interimaire(Nom,Email,Ville,Agence,DateNaiss) VALUES ('" + Interimaire.Nom + "' , '" + Interimaire.Email
                        + "' , '" + Interimaire.Ville + "' , '"+DropDownList2.SelectedValue +"' , '" + Interimaire.DateNaissance + "')";
                    SqlDataReader SqlReader = SQLCmd.ExecuteReader();

                    SqlC.Close();
       
                    //rédirection vers la page default.aspx avec comme param "id=2" pour le message de confirmation
                    Response.Redirect("~/Page_aspx/Default.aspx?id=" + (int)Utils.TypePage.ajout);

                }
                else
                    MessageDate.Text = "saisie incorrecte";
            }
            else
            {
                MessageNom.Text = "Saisie incorrecte";
            }
        }

        protected void reset_Click(object sender, EventArgs e)
        {
        }

        protected void Unnamed2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}
    }
}
